from OpenSSL.rand import status
from django.shortcuts import render

# Create your views here.
# from rest_framework.views import APIView
# from rest_framework.response import Response
# from django.db.models import Q
# from rest_framework.pagination import PageNumberPagination
# from rest_framework.viewsets import ModelViewSet
# from django_filters.rest_framework import DjangoFilterBackend
# from django_filters import filters,FilterSet
# from django.core.files.storage import default_storage
#
#
# from sympy.polys.polyconfig import query
#
# from backend import models, serializer


# class Loginview(APIView):
#     def post(self,request):
#         username=request.data.get('username')
#         password=request.data.get('password')
#         query=Q(username=username)
#         info=models.UsersModel.objects.filter(query).first()
#         if not info:
#             return Response({'code': 400, 'msg': '用户名不存在'})
#         if info.password==password:
#             return Response({'code': 400, 'msg': '密码成功'})
#         else:
#             return Response({'code': 400, 'msg': '密码错误'})


from rest_framework.views import APIView
from rest_framework.response import Response
from .models import UsersModel
from rest_framework.permissions import IsAuthenticated

class AdminLoginView(APIView):

    def post(self, request):
        print('request.data:', request.data)
        username = request.data.get('username')
        admin_password = request.data.get('admin_password')
        game = request.data.get('game')
        print('username:', username)
        print('admin_password:', admin_password)
        print('game:', game)
        if not username or not admin_password or not game:
            return Response({'code': 400, 'msg': '参数缺失'})
        admin = UsersModel.objects.filter(username=username, game=game).first()
        if not admin:
            return Response({'code': 400, 'msg': '管理员不存在'})
        if str(admin.admin_password) == str(admin_password):
            return Response({'code': 200, 'msg': '管理员登录成功'})

        else:
            return Response({'code': 400, 'msg': '密码错误'})
        
        



class AdminInfoView(APIView):             #后端接口返回管理员信息
    permission_classes = [IsAuthenticated]  # 只允许登录用户访问

    def get(self, request):
        # 假设你用JWT或Session，request.user就是管理员
        return Response({
            'username': request.user.username,
            'role': 'admin'
        })
